package kr.saeildamil.androidstudy_second.fragment.common;

import kr.saeildamil.androidstudy_second.R;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CounterFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/*
		  방법1 View root = inflater.inflate(R.layout.ch20_counterfragment,
		  container);
		 
		 final TextView textCounter = (TextView) root
		  .findViewById(R.id.counterfragment_txtcounter);
		 
		 root.findViewById(R.id.counterfragment_btn).setOnClickListener( new
		 OnClickListener() { public void onClick(View v) { int count =
		 Integer.parseInt(textCounter.getText() .toString());
		 textCounter.setText(Integer.toString(count + 1)); } });
		 */

		/*
		 //방법2 LinearLayout root = new LinearLayout(getActivity());
		  root.setOrientation(LinearLayout.VERTICAL);
		  root.setBackgroundColor(Color.YELLOW);
		  root.setGravity(Gravity.CENTER_HORIZONTAL);
		  
		  final TextView textcounter = new TextView(getActivity());
		  textcounter.setText("0"); textcounter.setTextColor(Color.RED);
		  textcounter.setTextSize(40);
		  
		  Button btnIncrease = new Button(getActivity());
		  btnIncrease.setText("Increase");
		  
		  LinearLayout.LayoutParams param = new
		  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
		  LinearLayout.LayoutParams.WRAP_CONTENT);
		  
		  root.addView(textcounter, param); root.addView(btnIncrease, param);
		  
		  btnIncrease.setOnClickListener(new OnClickListener() {
		  
		  @Override public void onClick(View v) { int count =
		  Integer.parseInt(textcounter.getText().toString());
		  textcounter.setText(Integer.toString(count+1)); } });
		 */

		/*// 방법3
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		Button btnIncrease = (Button) getActivity().findViewById(R.id.counterfragment_btn);
		final TextView textcounter = (TextView) getActivity().findViewById(R.id.counterfragment_txtcounter);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
						    textcounter.setText(Integer.toString(count+1));
			}
		});*/
		
		
		//방법4 onActivityCreate에서 구현
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		
		return root;
	}//end of method onCreate

	//방법4 onActivityCreate에서 구현 getActivity()
	//방법5 onActivityCreate에서 구현 getView()
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease = (Button) getView().findViewById(R.id.counterfragment_btn);
		final TextView textcounter = (TextView) getView().findViewById(R.id.counterfragment_txtcounter);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
						    textcounter.setText(Integer.toString(count+1));
			}
		});
	}
}
